<?php

    require_once("lib/includes.php");

	$action = new Action();

	echo json_encode(
        $action->Invoke(function(JSONReply $reply)
        {

            $gameId = (int)$_POST['gameid'];
            $answer = (int)$_POST['answer'];

            DB::Connect();

            Queries::UpdateLastActivity($gameId);

            // get the question index

            $stmt = DB::Query("select count(*) as numQuestions from GamesQuestions where GameID = $gameId");
            $result = $stmt->fetch_assoc();
            $stmt->close();

            $questionIndex = $result['numQuestions'] + 1;

            // not the first question, so answer the previous
            if ($questionIndex > 1)
            {
                $prevQuestionIndex = $questionIndex - 1;

                // set the answer to this question
                DB::Query("update GamesQuestions set GivenAnswer = $answer where QuestionIndex = $prevQuestionIndex");

                // update the objects which match the answer to the previous question
                Queries::UpdateObjectsForInstance($gameId, $prevQuestionIndex, GivenAnswer::ToSQLColumn($answer));
            }

            if ($questionIndex > Constants::NormalNumberOfQuestionsPerGame) // give the answer
            {
                $stmt = Queries::GetTopGuesses($gameId, (int)0, (int)1);

                $answer = $stmt->fetch_assoc();

                $stmt->close();

                Queries::SetGuessedObject($gameId, $answer['ObjectID']);

                $jsonReturn = array(
                        "ResponseType" => ResponseType::Result,
                        "ObjectName" => $answer['Name'],
                        "ObjectId" => $answer['ObjectID']
                      );

                $reply->SetData($jsonReturn);
            }
            else // ask the next question
            {
                $stmt = DB::Query
                            ("
                                select
                                    MAX(Instance.Fitness * ObjQuestion.Fitness) as `MAX`,
                                    ObjQuestion.QuestionID,
                                    Question.Question
                                from
                                    GamesInstances as Instance
                                inner join
                                    ObjectsQuestions as ObjQuestion on ObjQuestion.ObjectID = Instance.ObjectID
                                    and Instance.GameID = $gameId
                                    and ObjQuestion.QuestionID not in (	select
                                                                            GQ.QuestionID
                                                                        from
                                                                            GamesQuestions as GQ
                                                                        where
                                                                            GQ.GameID = $gameId
                                                                       )
                                inner join
                                    Questions as Question on Question.QuestionID = ObjQuestion.QuestionID
                                group by
                                    ObjQuestion.ObjectID,
                                    ObjQuestion.QuestionID,
                                    Question.Question
                                order by
                                    `MAX` desc
                                limit
                                    0, 1
                            ");

                $question = $stmt->fetch_assoc();

                $stmt->close();

                if ($question)
                {
                    // insert the question to this game
                    $stmt = DB::Prepare("insert into GamesQuestions (GameID, QuestionID, QuestionIndex) values (?, ?, ?)");
                    $stmt->bind_param('iii', $gameId, $question['QuestionID'], $questionIndex);
                    $stmt->execute();

                    $stmt->close();

                    $jsonReturn = array(
                                    "ResponseType" => ResponseType::Question,
                                    "Question" => $question['Question']
                                  );

                    $reply->SetData($jsonReturn);
                }
            }

            DB::Disconnect();

    	}));

?>